﻿using System;
using PMngr.Infrastructure;

namespace PMngr.Data
{
    public class RepositoryFactory : IRepositoryFactory
    {
        private readonly string _provider;
        private IRepository _repository;

        public RepositoryFactory(string provider)
        {
            _provider = provider;
        }

        private void CreateDefaultDatabase()
        {
            #region ItemCodes initialization
            // Options Management
            _repository.Add(new ItemBase { Name = "UnitType", Order = 1, Type = "OptionType", Description = "单位区域" });
            _repository.Add(new ItemBase { Name = "UnitKind", Order = 2, Type = "OptionType", Description = "单位性质" });
            _repository.Add(new ItemBase { Name = "PolicyFace", Order = 3, Type = "OptionType", Description = "政治面貌" });
            _repository.Add(new ItemBase { Name = "SchoolRecord", Order = 4, Type = "OptionType", Description = "学历" });
            _repository.Add(new ItemBase { Name = "SchoolLevel", Order = 5, Type = "OptionType", Description = "学位" });
            _repository.Add(new ItemBase { Name = "Identity", Order = 6, Type = "OptionType", Description = "本人身份" });
            _repository.Add(new ItemBase { Name = "TechGrade", Order = 7, Type = "OptionType", Description = "技术等级" });
            _repository.Add(new ItemBase { Name = "TransCause", Order = 8, Type = "OptionType", Description = "调动理由" });           
            // 单位区域分为：市直、县区、外市、外省，共四类
            var uk1 = new ItemBase { Name = "市直", Order = 1, Type = "UnitType", Description = "市直" };
            _repository.Add(uk1);
            var uk2 = new ItemBase { Name = "县区", Order = 2, Type = "UnitType", Description = "县区" };
            _repository.Add(uk2);
            _repository.Add(new ItemBase { Name = "省直", Order = 3, Type = "UnitType", Description = "省直" });
            var uk3 = new ItemBase { Name = "外市", Order = 4, Type = "UnitType", Description = "外市" };
            _repository.Add(uk3);
            _repository.Add(new ItemBase {Name = "外省", Order = 5, Type = "UnitType", Description = "外省"});
            // 单位性质分为：机关、参公事业、全额事业、差额事业、自收自支事业、国有企业、人力资源市场、其他，共八类
            var ut1 = new ItemBase {Name = "机关", Order = 1, Type = "UnitKind", Description = "机关"};
            _repository.Add(ut1);
            var ut2 = new ItemBase {Name = "参公事业", Order = 2, Type = "UnitKind", Description = "参公事业"};
            _repository.Add(ut2);
            _repository.Add(new ItemBase {Name = "全额事业", Order = 3, Type = "UnitKind", Description = "全额事业"});
            _repository.Add(new ItemBase {Name = "差额事业", Order = 4, Type = "UnitKind", Description = "差额事业"});
            _repository.Add(new ItemBase {Name = "自收自支事业", Order = 5, Type = "UnitKind", Description = "自收自支事业"});
            _repository.Add(new ItemBase {Name = "国有企业", Order = 6, Type = "UnitKind", Description = "国有企业"});
            _repository.Add(new ItemBase {Name = "人力资源市场", Order = 7, Type = "UnitKind", Description = "人力资源市场"});
            _repository.Add(new ItemBase {Name = "非国有单位", Order = 8, Type = "UnitKind", Description = "非国有单位" });
            _repository.Add(new ItemBase {Name = "其他", Order = 9, Type = "UnitKind", Description = "其他"});
            // 政治面貌
            _repository.Add(new ItemBase {Name = "党员", Order = 1, Type = "PolicyFace"});
            _repository.Add(new ItemBase {Name = "民主党派", Order = 2, Type = "PolicyFace"});
            _repository.Add(new ItemBase {Name = "群众", Order = 3, Type = "PolicyFace"});
            _repository.Add(new ItemBase {Name = "团员", Order = 4, Type = "PolicyFace"});
            // 学历 
            _repository.Add(new ItemBase {Name = "研究生", Order = 1, Type = "SchoolRecord"});
            _repository.Add(new ItemBase {Name = "大学本科", Order = 2, Type = "SchoolRecord"});
            _repository.Add(new ItemBase {Name = "大学专科", Order = 3, Type = "SchoolRecord"});
            _repository.Add(new ItemBase {Name = "中专", Order = 4, Type = "SchoolRecord"});
            _repository.Add(new ItemBase {Name = "高中及以下", Order = 5, Type = "SchoolRecord"});
            // 学位 
            _repository.Add(new ItemBase {Name = "学士", Order = 1, Type = "SchoolLevel"});
            _repository.Add(new ItemBase {Name = "硕士", Order = 2, Type = "SchoolLevel"});
            _repository.Add(new ItemBase {Name = "博士", Order = 3, Type = "SchoolLevel"});
            // 本人身份 
            _repository.Add(new ItemBase {Name = "干部", Order = 1, Type = "Identity"});
            _repository.Add(new ItemBase {Name = "聘用制干部", Order = 2, Type = "Identity"});
            _repository.Add(new ItemBase {Name = "工勤人员", Order = 3, Type = "Identity"});
            // 技术等级
            _repository.Add(new ItemBase {Name = "普通工", Order = 1, Type = "TechGrade"});
            _repository.Add(new ItemBase {Name = "初级工", Order = 2, Type = "TechGrade"});
            _repository.Add(new ItemBase {Name = "中级工", Order = 3, Type = "TechGrade"});
            _repository.Add(new ItemBase {Name = "高级工", Order = 4, Type = "TechGrade"});
            _repository.Add(new ItemBase {Name = "技师", Order = 5, Type = "TechGrade"});
            _repository.Add(new ItemBase {Name = "高级技师", Order = 6, Type = "TechGrade"});
            // 调动理由
            _repository.Add(new ItemBase {Name = "工作需要", Order = 1, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "公开招聘", Order = 2, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "高级职称", Order = 3, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "高学历人才", Order = 4, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "随军家属", Order = 5, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "整体划转", Order = 6, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "任职", Order = 7, Type = "TransCause"});
            _repository.Add(new ItemBase {Name = "调任", Order = 8, Type = "TransCause"});          
            #endregion

            #region User initialization
            var user = new User
            {
                Id = Guid.NewGuid(),
                Name = "admin",
                CreateDate = DateTime.Now,
                EncryptPwd = "admin".MD5Encrypt(),
                Role = 1
            };
            _repository.Add(user);
            #endregion

            #region 测试数据
            //// 主管部门测试数据
            //_repository.Add<Unit>(new Unit { Name = "主管部门A", Description = "测试数据", UnitType = uk3 });
            //_repository.Add<Unit>(new Unit { Name = "主管部门B", Description = "测试数据", UnitType = uk1 });
            //_repository.Add<Unit>(new Unit { Name = "主管部门C", Description = "测试数据", UnitType = uk2 });
            //_repository.Add<Unit>(new Unit { Name = "主管部门D", Description = "测试数据", UnitType = uk3 });
            //var zu = new Unit { Name = "北京外企集团", Description = "测试数据", UnitType = uk3 };
            //_repository.Add<Unit>(zu);
            //// 单位测试数据
            //_repository.Add<Unit>(new Unit { Name = "霍尼韦尔（北京）技术研发实验有限公司", Description = "Honeywell", ParentUnit = zu, UnitType = ut1 });
            //_repository.Add<Unit>(new Unit { Name = "三星数据系统（中国）有限公司", Description = "Samsung SDSC", ParentUnit = zu, UnitType = ut2 });
            #endregion           

            _repository.CommitChanges();       
        }

        public IRepository CreateRepository()
        {
            switch (_provider)
            {
                case "MSSQL":
                    {
                        //const string connectionString = "Data Source=.\\SQLEXPRESS;Initial Catalog=OpenPOS_MSSQL;Integrated Security=True;";
                        if (!BuildEFSession())
                            CreateDefaultDatabase();
                    }
                    break;
                case "MongoDB":
                   
                    break;
                case "DB4O":
                   
                    break;
                default:
                    {
                        //const string connectionString = "Data Source=OpenPOS.sdf";
                        if (!BuildEFSession())
                        {
                            CreateDefaultDatabase();
                        }
                    }
                    break;
            }

            return _repository;
        }

        #region EF

        private bool BuildEFSession()
        {
            var context = new EF.UnitContext();
            var dbExists = context.Database.Exists();
            _repository = new EF.EFRepository(context);
            return dbExists;
        }

        #endregion
    }
}
